﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;

namespace AugustSql
{
    public class DateTimePicker : Control
    {
        List<int> hours, mins, secs;
        ListBox con_hours, con_mins, con_secs;
        Calendar calendar;
        ToggleButton toggle;
        Button btnOk;
        static DateTimePicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DateTimePicker), new FrameworkPropertyMetadata(typeof(DateTimePicker)));
        }

        public DateTimePicker()
        {
            Loaded += DateTimePicker_Loaded;
        }

        private void DateTimePicker_Loaded(object sender, RoutedEventArgs e)
        {
            DisPlayValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            hours = new List<int>();
            mins = new List<int>();
            secs = new List<int>();
            con_hours = GetTemplateChild("PART_hours") as ListBox;
            con_mins = GetTemplateChild("PART_mins") as ListBox;
            con_secs = GetTemplateChild("PART_secs") as ListBox;
            calendar = GetTemplateChild("PART_calendar") as Calendar;
            btnOk = GetTemplateChild("PART_Confirm") as Button;
            toggle = GetTemplateChild("PART_togg") as ToggleButton;
            for (int i = 0; i < 60; i++)
            {
                if (i < 24)
                {
                    hours.Add(i);
                }
                mins.Add(i);
                secs.Add(i);
            }
            con_hours.ItemsSource = hours;
            con_mins.ItemsSource = mins;
            con_secs.ItemsSource = secs;
            btnOk.Click -= BtnOk_Click;
            btnOk.Click += BtnOk_Click;
            calendar.PreviewMouseUp += Calendar_PreviewMouseUp;
        }

        private void Calendar_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (Mouse.Captured is CalendarItem)
            {
                Mouse.Capture(null);
            }
            //calendar?.ReleaseStylusCapture();
        }

        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            DateTime? time = calendar.SelectedDate;
            if (time == null)
                time = DateTime.Today;
            var span = new TimeSpan();
            if (con_hours.SelectedItem is int hour)
            {
                span += new TimeSpan(hour, 0, 0);
            }
            if (con_mins.SelectedItem is int mins)
            {
                span += new TimeSpan(0, mins, 0);
            }
            if (con_secs.SelectedItem is int sec)
            {
                span += new TimeSpan(0, 0, sec);
            }
            EditTimeValue = time + span;
            toggle.IsChecked = false;
        }

        public DateTime? EditTimeValue
        {
            get { return (DateTime)GetValue(EditTimeValueProperty); }
            set
            {
                SetValue(EditTimeValueProperty, value);
                DisPlayValue = value?.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        // Using a DependencyProperty as the backing store for EditTimeValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EditTimeValueProperty =
            DependencyProperty.Register("EditTimeValue", typeof(DateTime), typeof(DateTimePicker), new PropertyMetadata(DateTime.Now));




        public string DisPlayValue
        {
            get { return (string)GetValue(DisPlayValueProperty); }
            set { SetValue(DisPlayValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DisPlayValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DisPlayValueProperty =
            DependencyProperty.Register("DisPlayValue", typeof(string), typeof(DateTimePicker), new PropertyMetadata(default));

    }
}
